#include "luster_test.hh"

#include <inttypes.h>
#include <stdint.h>

#include "luster_log.hh"

namespace luster {

#define TEST_MAP(_) \
	_(StringGet) \
	_(StringAppend) \
	_(StringSwap) \
	_(vm_basic) \
	_(vm_call) \
	_(vm_gcd) \
	_(parse_token) \
	_(Cmd)

#define _(M_name) extern "C" ::luster::TestCase luster__testcase__##M_name;
TEST_MAP(_)
#undef _

void luster_cmd_test(void) {
	TestCase *testcases[] = {
#define _(M_name) &luster__testcase__##M_name,
		TEST_MAP(_)
#undef _
			NULL,
	};

	UInt case_fail{};
	UInt error_count{};

	for (uintptr_t i = 0; testcases[i] != NULL; ++i) {
		TestCase const *t = testcases[i];

		LOG(INFO) << "run test: " << t->file << ":" << t->line << " "
			  << t->name << LOGEND;

		TestContext ctx;
		t->func(&ctx);

		if (ctx.error_count != 0) {
			case_fail += 1;
			error_count += ctx.error_count;

			LOG(ERROR)
				<< "test `" << t->name << "' failed" << LOGEND;
		}
	}

	if (case_fail > 0) {
		LOG(FATAL) << case_fail << " tests failed, " << error_count
			   << " errors in total" << LOGEND;
	}
}

} // namespace luster
